package sqlite.com.sqlite;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class MyBaseAdapter extends BaseAdapter {
    public static byte[] bytes;
    private List<Music> musicList;
    private LayoutInflater mInflater;
    public ArrayList<byte[]> img_res;

    public MyBaseAdapter(Context context, List<Music> list,ArrayList<byte[]> img_res) {
        this.musicList = list;
        this.mInflater = LayoutInflater.from(context);
        img_res.add(img_res.get(img_res.size()-1));
        this.img_res=img_res;
    }

    @Override
    public int getCount() {
        return musicList.size();
    }

    @Override
    public Object getItem(int position) {
        return musicList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        //如果view未被实例化过，缓存池中没有对应的缓存
        if (convertView == null) {
            viewHolder = new ViewHolder();
            // 由于我们只需要将XML转化为View，并不涉及到具体的布局，所以第二个参数通常设置为null
            convertView = mInflater.inflate(R.layout.item, null);

            //对viewHolder的属性进行赋值
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image_view);
            viewHolder.title = (TextView) convertView.findViewById(R.id.my_title);
            viewHolder.artist = (TextView) convertView.findViewById(R.id.artist);
            viewHolder.duration = (TextView) convertView.findViewById(R.id.duration);
            //通过setTag将convertView与viewHolder关联
            convertView.setTag(viewHolder);
        }else{//如果缓存池中有对应的view缓存，则直接通过getTag取出viewHolder
            viewHolder = (ViewHolder) convertView.getTag();
        }
        // 取出bean对象
        Music music = musicList.get(position);

        // 设置控件的数据
        Bitmap bitmap1 = BitmapFactory.decodeByteArray(this.img_res.get(music.getId()), 0,this.img_res.get(music.getId()).length);
        viewHolder.imageView.setImageBitmap(bitmap1);
        viewHolder.title.setText(music.title);
        viewHolder.artist.setText(music.artist);
        viewHolder.duration.setText(music.duration);

        return convertView;



        /*

//        ViewHolder viewHolder=new ViewHolder();
        View view = mInflater.inflate(R.layout.item, null);

        ImageView imageView = (ImageView) view.findViewById(R.id.image_view);
        TextView title = view.findViewById(R.id.my_title);
        TextView artist = view.findViewById(R.id.artist);
        TextView duration = view.findViewById(R.id.duration);

        Music music = musicList.get(position);

        //        try {
        //            Bitmap bitmap=BitmapFactory.decodeStream(new URL(music.thumb_url).openConnection().getInputStream());
        //            imageView.setImageBitmap(bitmap);
        //        } catch (IOException e) {
        //            e.printStackTrace();
        //        }
        //        viewHolder.imageView.setImageResource(R.drawable.happy);
        Bitmap bitmap1 = BitmapFactory.decodeByteArray(this.img_res.get(music.getId()), 0,this.img_res.get(music.getId()).length);
        imageView.setImageBitmap(bitmap1);
        title.setText(music.title);
        artist.setText(music.artist);
        duration.setText(music.duration);

        return view;

         */


    }

    class ViewHolder {
        ImageView imageView;
        TextView title;
        TextView artist;
        TextView duration;
    }
    public class myThread extends Thread{
        String path;
        public myThread(String path){
                this.path=path;
        }
        @Override
        public void run() {
            super.run();
            URL url = null;
            try {
                url = new URL(path);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                InputStream is = connection.getInputStream();
                ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len;
                while ((len = is.read(buffer)) != -1) {
                    byteArrayOut.write(buffer, 0, len);
                }
                changeState(byteArrayOut);
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

//            return byteArrayOut.toByteArray();
        }
        public void changeState(ByteArrayOutputStream byteArrayOut){
            MyBaseAdapter.bytes=byteArrayOut.toByteArray();
        }
    }
}
